home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 610 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.6 KB

  1. From: "linh (l.) dang" <linhd@bnr.ca>
  2. Message-ID: <gknwx54om1a.fsf@bmtlh30.nortel.ca>
  3. X-Original-Date: Fri, 1 Mar 1996 10:44:49 -0500
  4. Path: in2.uu.net!bounce-back
  5. Date: 01 Mar 96 17:17:32 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. References: <4h5j7b$1ur@bcarh8ab.bnr.ca>
  8. Subject: Re: Generic Object Callbacks
  9. Newsgroups: comp.std.c++
  10. Organization: The entity formerly know as BNR
  11. X-Newsreader: Gnus v5.1
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMTcxT+EDnX0m9pzZAQGJewF+IXrd5wDWjD/ZTlZYY01/Uq3DvA1BxHqs
  14.     X3erjHLfcEPksawXdSsuYPxJ504SneIF
  15.     =Podx
  16.  
  17. >> "ian" == "ian (i ) willmott" <willmott@bnr.ca> writes:
  18.  
  19.  ian> The point is that the "driver" shouldn't have to know the type
  20.  ian> of the object the callback is directed at. This is particularly
  21.  ian> important when the "driver" issuing the callback is a library
  22.  ian> API, such as Motif; you do not want the library to have to know
  23.  ian> about application data types and you do not want to constrain
  24.  ian> the application to use a single callback object type that is
  25.  ian> defined by the library. The point of the proposal is that there
  26.  ian> is a simple extension to the language which would accomplish
  27.  ian> this in a type-safe way.
  28.  
  29. I don't understand why the example in the C++ Report is not sufficient
  30. for you. Any way if you want a simpler (i.e. less sophisticated
  31. version, here it is and please, no more extension !!!!
  32.  
  33. //
  34. // library side ===========================================
  35. //
  36. class Callback
  37. {
  38. public:
  39.   virtual void call() = 0;
  40. };
  41.  
  42. void foo(Callback& cb)
  43. {
  44.   cb.call();            // callback
  45. }
  46.  
  47. template<class ClientInfo>
  48. class GenCallback : public Callback
  49. {
  50. public:
  51.   ClientInfo client;
  52.   
  53.   virtual void call()
  54.   {
  55.     ((client.object)->*(client.method))(client.data);
  56.   }
  57. };
  58.  
  59. //
  60. // application side ===========================================
  61. //
  62.  
  63. class MyObject            // your object here
  64. {
  65. public:
  66.   struct arg { int i; double f; };
  67.   virtual void bar(arg&);
  68. };
  69.  
  70. typedef void (MyObject::*MyObjectMethod)(MyObject::arg&);
  71.  
  72. struct MyCBInfo
  73. {
  74.   MyObject* object;
  75.   MyObjectMethod method;
  76.   MyObject::arg data;
  77. };
  78.  
  79. main()
  80. {
  81.   MyObject o;
  82.   GenCallback<MyCBInfo>* p = new GenCallback<MyCBInfo>;
  83.   p->client.object = &o;
  84.   p->client.method = MyObject::bar;
  85.   p->client.data.i = 1;
  86.   p->client.data.f = 0.1;
  87.   foo(*p);
  88.   return 0;
  89. }
  90.  
  91.  
  92.  
  93. --------------
  94. L.D.
  95. ---
  96. [ To submit articles: try just posting with your news-reader.
  97.                       If that fails, use mailto:std-c++@ncar.ucar.edu
  98.   FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  99.   Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  100.   Comments? mailto:std-c++-request@ncar.ucar.edu.
  101. ]
  102.